package com.ordering.action.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.ordering.action.BaseAction;
import com.ordering.constant.ResultResponse;
import com.ordering.entity.Order;
import com.ordering.entity.Page;
import com.ordering.service.order.OrderService;
import com.ordering.service.order.impl.OrderServiceImpl;

public class AdminIndexAction extends BaseAction{
	
    //输入
	private int orderId;
	
	private int page = 1;
	
	private long totalRecord = 0;
	
	private int toStatus ;
	
	private int status = 1;
	//输出
	private Order order;
	
	private List<Order> resultList;
	
	
	private boolean result = false;
	
	private String errMessage;
	
	OrderService orderService = new OrderServiceImpl();
	
	public String index(){
		return "index";
	}
	
	public String list(){
		try
        {
        	ResultResponse re = new ResultResponse();
        	if(order == null){
        		order = new Order();
        	}
        	order.setStatus(status);
        	re = orderService.count(order);
        	totalRecord = (Long)re.getResult();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return FAIL;
        }
        return "list";
	}
	
	public String page(){
		try
        {
			if(order == null){
        		order = new Order();
        	}
			order.setStatus(status);
        	ResultResponse re = orderService.get4page(order, new Page(page,PAGE_SIZE));
        	resultList = (ArrayList<Order>)re.getResult();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return FAIL;
        }
        
        return "page";
	}
	
	public String view(){
        try
        {
            ResultResponse re = orderService.getById(orderId);
            order = (Order)re.getResult();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return FAIL;
        }
        
        return "view";
    }
	
	
	public void changeStatus(){
		JSONObject json = new JSONObject();
		
		try
        {
			Order theOrder = new Order();
			theOrder.setId(orderId);
			theOrder.setStatus(toStatus);
            ResultResponse re = orderService.updateStatus(theOrder);
            if(re.getCode() == ResultResponse.CODE_SUCCESS){
            	result = (Boolean)re.getResult();
            }else{
            	json.put("message", re.getMessage());
            }
            json.put("status", result);
            outPutMessage(json.toString());
        }
        catch (Exception e)
        {
            e.printStackTrace();

        }
		
	}
	
	//将json以输出流方式输出
	private void outPutMessage(String jsonStr) throws IOException{
	    String contentType = "text/html;charset=utf-8";
		//指定输出内容类型和编码  
        ServletActionContext.getResponse().setContentType(contentType);   
        //获取输出流，然后使用  
        PrintWriter out = ServletActionContext.getResponse().getWriter();
        out.print(jsonStr);
        out.flush();  
        out.close(); 
	}
	
	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public long getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(long totalRecord) {
		this.totalRecord = totalRecord;
	}

	public List<Order> getResultList() {
		return resultList;
	}

	public void setResultList(List<Order> resultList) {
		this.resultList = resultList;
	}

    public int getOrderId()
    {
        return orderId;
    }

    public void setOrderId(int orderId)
    {
        this.orderId = orderId;
    }

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public boolean isResult() {
		return result;
	}

	public void setResult(boolean result) {
		this.result = result;
	}

	public int getToStatus() {
		return toStatus;
	}

	public void setToStatus(int toStatus) {
		this.toStatus = toStatus;
	}

	public String getErrMessage() {
		return errMessage;
	}

	public void setErrMessage(String errMessage) {
		this.errMessage = errMessage;
	}
}
